/**
 * @file
 * @author dousha
 * @date 2020
 */
.thumb
.syntax unified

.section .data
lr_value: .word 0xfffffffd

.section .text.ServiceHandler
.type  service_handler, %function
.extern currentTask
.extern task_reschedule_current_task
.extern task_cleanup
.extern task_pend_switch
.global service_handler

.thumb_func
/**
 * 系统调用处理程序。
 *
 * @invokedBy{startup.s}
 */
service_handler:
	teq r0, #0
	beq dive_into_user_program
	teq r0, #1
	beq task_terminate
	teq r0, #2
	beq task_yield
	teq r0, #0x11
	beq task_terminate_unexpcetedly
	// 如果之后有更多的系统调用，在此行之上追加
	// 未知的系统调用，直接返回
	b service_done
task_terminate_unexpcetedly:
task_terminate:
	bl task_cleanup
	b service_done
task_yield:
	push { lr }
	bl task_reschedule_current_task
	bl task_pend_switch
	pop { lr }
	b service_done
dive_into_user_program:
	ldr r3, =currentTask
	ldr r2, [r3]
	ldr r0, [r2]
	ldmia r0!, {r4-r11,lr}
	msr psp, r0
	isb
service_done:
	bx lr
